package pswvclass.pswvandroid2.com.user;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.Login;
import pswvclass.pswvandroid2.com.PswvActivity;
import pswvclass.pswvandroid2.com.util.DBHelper;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/12/18.
 */
public class EditPassword extends AppCompatActivity implements View.OnClickListener {

    private String tag = "EditPassword";
    private TextView edit_password;
    private int userId;
    private Button edit_sub;
    private DBHelper db;
    private SQLiteDatabase sd;
    protected static final int REGISTER_SUCCESS = 0;
    protected static final int ERROR = 1;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.pswv_password_edit);
        Bundle data = getIntent().getBundleExtra("data");
        userId = data.getInt("userId");
        edit_password = (TextView) findViewById(R.id.edit_password);
        edit_sub = (Button) findViewById(R.id.edit_sub);
        edit_sub.setOnClickListener(this);
    }


    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case REGISTER_SUCCESS:
                    Intent intent = new Intent();
                    intent.setClass(EditPassword.this, Login.class);
                    startActivity(intent);
                    break;
                case ERROR:
                    break;
                default:
                    break;
            }

        }

        ;

    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.edit_sub:
                final String password = edit_password.getText().toString().trim();
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                new Thread() {
                    public void run() {
                        try {
                            HttpUtils hu = new HttpUtils();
                            String data2;
                            final String src = getResources().getString(R.string.src_mysql) + "pswv/api/userAction/add";
                            final Map<String, String> map1 = new HashMap<String, String>();
                            map1.put("newPassword", password);
                            map1.put("id", userId + "");
                            data2 = hu.submitPostData(src, map1, "utf-8");
                            JSONObject jsonObj = new JSONObject(data2);
                            int result = Integer.parseInt(jsonObj.getString("code"));
                            if (result == 0) {
                                db = new DBHelper(EditPassword.this);
                                sd = db.getWritableDatabase();
                                ContentValues values1 = new ContentValues();
                                values1.put(DBHelper.STATE, 0);
                                sd.update("user", values1, "userId=? and state=1", new String[]{userId + ""});
                                Message msg = Message.obtain();
                                msg.what = REGISTER_SUCCESS;
                                mHandler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Message msg = Message.obtain();
                            msg.what = ERROR;
                            mHandler.sendMessage(msg);
                            Log.v(tag, e + "");
                        }
                    }
                }.start();
                break;
            default:
                break;
        }
    }
}
